const tool = require('./tool.js');
var params = {};
//一轮评论多少个视频
maxplNum = 20
//随机停留的时间
randomTime = 15
//重复进入视频的次数
var enterVedioCount = 0;
//评论失败的次数
var failCount = 0;
function main(p) {
    params = p;
    init()
}

//启动
function init() {
    tool.restartApp("快手")
    sleep(5000)
    //关闭弹窗
    closeLinkPopup()
    //进入搜索页面
    tool.log("开始搜索")
    if (click(1139, 189)) {
        tool.log("点击搜索成功")
        sleep(1000)
        tool.log("搜索词：" + params.searchWord)
        setText(random(1, 10) + params.searchWord + random(1, 10))
        sleep(2500)
        tool.clickText("搜索")
        sleep(5000)
        enterSearchVideo()
    } else {
        tool.log("点击搜索失败")
        init()
    }
}
//关闭链接弹窗
function closeLinkPopup () {
    //弹窗关键字
    let popupKeywords = ["同意并继续","同意,并领新手红包", "去看看","我知道了","稍后","取消","忽略"]
    //遍历关键字
    for (let i = 0; i < popupKeywords.length; i++) {
        if (text(popupKeywords[i]).exists()) {
            click(popupKeywords[i])
            sleep(2000)
        }
    }
}
//随选条件
function enterSearchVideo() {
    click(1131, 343)
    sleep(2000)
    click("最新发布")
    sleep(500)
    click("未看过")
    sleep(500)
    click("确定")
    sleep(5000)
    click(313, 736)
    sleep(2000)
    pinglun()
}


//开始评论
function pinglun() {
    tool.log("开始评论")
    for (let i = 0; i < maxplNum; i++) {
        if (tool.isChange()) {
            tool.log("当前视频未变化")
            enterVedioCount++
            if (enterVedioCount >= 2) {
                tool.log("评论次数过多，退出评论")
                enterVedioCount = 0
                init()
                return
            }
        } else {
            enterVedioCount = 0
            if (sendPinglun(params.pinglun)) {
                tool.log("评论成功")
                failCount = 0
                sleep(1000, randomTime * 1000)
            } else {
                tool.log("评论失败")
                back()
                failCount++
                if (failCount >= 3) {
                    failCount = 0
                    init()
                    return
                }
            }
            press(800, 1500, 100)
            sleep(1000)
            if (id("top_bar_user_name").exists()) {   //是否进入图文详情
                back()
                sleep(1000)
            }
            sleep(200)
        }
        //截取当前屏幕
        captureScreen(`./image/dqpm.png`);
        nextLive(3000)
    }
    init()
}


//打开评论输入框
function openPinglun() {
    //是否打开评论
    let isOpen = {
        state: false,
        page: 0
    };
    if (textContains("发条有爱评论~").exists() && click("发条有爱评论~")) {
        isOpen.state = true
    } else {
        click(1135, 1769)
        sleep(1000)
        if (textContains("发条有爱评论~").exists() && click("发条有爱评论~")) {
            isOpen.state = true
            isOpen.page = 1
        } else if (textContains("快来抢首评吧").exists() && click("快来抢首评吧")) {
            isOpen.state = true
            isOpen.page = 1
        }
    }
    sleep(1500)
    return isOpen;
}


//发送评论
function sendPinglun(text) {
    //发送状态
    let sendState = false;
    let isOpen = openPinglun()
    if (isOpen.state) {
        setText(text)    //输入评论
        sleep(2000)
        if (click("发送")) {      //发送评论
            sleep(2000)
            if (isOpen > 0) {
                back()
            }
            sendState = true;
        }
    }
    return sendState;
}

//切换下一个直播间
function nextLive(time) {
    tool.log("切换下一个直播间")
    sleep(1000);
    let startX = random(200, 300)
    let startY = random(1500, 1600)
    let endX = random(300, 350)
    let endY = random(50, 100)
    swipe(startX, startY, endX, endY, 50)
    sleep(time)
}


exports.main = main;